﻿using Dm.parser;
using FrameWork.Core.Infrastructure;
using FrameWork.Data.Interface;
using FrameWork.Data.Mapping;
using SqlSugar.Extensions;
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace AncheStart.Converter
{
    public class ErrorCodeConverter : IValueConverter
    {
        public List<MiddleErrorCode> middleErrorCodes { get; set; }

        public ErrorCodeConverter()
        {
            //默认取第一组
            middleErrorCodes = EngineContext.Current.Resolve<IMiddleErrorCodeManager>().getByWhere(t => t.Group == 1).GetAwaiter().GetResult();
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                try
                {
                    //95 下位机未连接
                    MiddleErrorCode middleErrorCode= middleErrorCodes.FirstOrDefault(s => s.Key == value.ObjToInt());
                    if (middleErrorCode != null) 
                    {
                        return value + "-" + middleErrorCode.Value;
                    }
                  
                }
                catch (Exception ex)
                {
                    return "未定义错误码" + "-" + value;
                }
            }
            else
            {
                return "未定义错误码" + "-" + value;
            }
            return "未定义错误码" + "-" + value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}